<?php
class Candidate_video_model extends MY_Model {

    function __construct()
    {
        parent::__construct();
        $this->make_dirs();
    }
    
	function get_max_id($data = null)
    {
    	$this->db->select_max('id');
    	
    	if (isset($data['idmember']) && is_numeric($data['idmember'])) {
    		$this->db->where('candidate_video.idmember', $data['idmember']);
    	}
    	
    	return $this->db->get('candidate_video')->row()->id;
    }
    
    function get($data)
    {
    	if (isset($data['idvideo']) && is_numeric($data['idvideo'])) {
    		$this->db->where('candidate_video.id', $data['idvideo']);
    	}
    	
    	if (isset($data['idmember']) && is_numeric($data['idmember'])) {
    		$this->db->where('candidate_video.idmember', $data['idmember']);
    	}
    	
    	$this->db->order_by('candidate_video.idpresentation', 'desc');
    	$this->db->order_by('candidate_video.id', 'desc');
    	
    	return $this->db->get('candidate_video');
    }
    
	function update($data)
    {
    	$this->db->set('title', $data['title']);
    	$this->db->set('description', $data['description']);
		$this->db->where('id', $data['id']);
		
		$this->db->update('candidate_video');
    }
    
	function insert($data)
    {
    	$this->db->trans_begin();
    	
    	if (isset($data['image']) && $data['image'] != '')
    	{
	    	$extension = end(explode('.', $data['image_name']));
			
			$image_path = FCPATH . 'f/' . $data['idmember'] . '/vid';
			
			if (!is_dir($image_path))
			{
				mkdir($image_path);
			}
			
			$tmp_path = $data['tmp_path'];
			$tmp_file = $data['tmp_file'];
			
			$image_prefix = $this->get_max_id() . $tmp_file;
			
			$image = md5($image_prefix . '_image').'.'.$extension;
			$config = array();
	    	$config['image_library'] = 'gd2';
			$config['source_image'] = $tmp_file;
			$config['new_image'] = $image_path . '/' . $image;
			$config['maintain_ratio'] = TRUE;
	    	$config['width'] = 160;
			$config['height'] = 160;
			
			$this->load->library('image_lib', $config);
			$this->image_lib->resize();
			
			unlink($tmp_file);
			rmdir($tmp_path);
    	}
		
		$data_insert = array(
		   'image' => (isset($image) ? $image : ''),
		   'title' => (isset($data['title']) ? $data['title'] : ''),
		   'description' => (isset($data['description']) ? $data['description'] : ''),
		   'idmember' => $data['idmember'],
    	   'idopentok' => (isset($data['idopentok']) ? $data['idopentok'] : 'N'),
		   'link' => $data['link'],
		   'idpresentation' => (!$this->get(array('idmember' => $data['idmember']))->row() ? 'Y' : 'N')
		);
		
		$this->db->set('date', 'NOW()', false);
		$this->db->insert('candidate_video', $data_insert);
		
    	if ($this->db->trans_status() === false)
		{
		    $this->db->trans_rollback();
		    return false;
		}
		else
		{
		    $this->db->trans_commit();
		    return true;
		}
    }
    
	function delete($data)
    {
    	$this->db->trans_begin();
    	
    	$video_data = $this->get(array('idvideo' => $data['id']))->row();
    	
    	$data_delete = array(
		   'id' => $data['id']
		);
		
		$this->db->delete('candidate_video', $data_delete);
		
    	if ($this->db->trans_status() === false)
		{
		    $this->db->trans_rollback();
		    return false;
		}
		else
		{
		    $this->db->trans_commit();
		    $video_path = FCPATH . 'f/' . $this->session->userdata['idmember'] . '/vid';
		    $image_file = $video_path . '/' . $video_data->image; 
	    	if (file_exists($image_file))
	    	{
		    	unlink($image_file);
	    	}
		    return true;
		}
    }
    
	function update_presentation($data)
    {
    	$this->db->trans_begin();
    	
    	$this->db->set('idpresentation', 'N');
		$this->db->where('idmember', $this->session->userdata['idmember']);
		$this->db->update('candidate_video');
		
		$this->db->set('idpresentation', 'Y');
		$this->db->where('id', $data['id']);
		$this->db->update('candidate_video');
		
   		if ($this->db->trans_status() === false)
		{
		    $this->db->trans_rollback();
		    return false;
		}
		else
		{
		    $this->db->trans_commit();
		    return true;
		}
    }
}
?>